package org.gp.demo.file.xml;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

public class StAxDemo1 {
	public static void main(String[] args) {
		String urlStr = "http://www.w3c.org";
		URL url;
		try {
			url = new URL(urlStr);
			InputStream is = url.openStream();
			XMLInputFactory xif = XMLInputFactory.newInstance();
			XMLStreamReader parser = xif.createXMLStreamReader(is);
			while (parser.hasNext()) {
				int event = parser.next();
				if (event == XMLStreamConstants.START_ELEMENT) {
					if (parser.getLocalName().toLowerCase().equals("a")) {
						String href = parser.getAttributeValue(null, "href");
						if (null != href)
							System.out.println(href);
					}
				}
			}
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (XMLStreamException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
